<template>
    <div class="app-container">
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <!-- <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:role:add']">新增</el-button> -->
                <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    >新增区域
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="info"
                    plain
                    icon="el-icon-sort"
                    size="mini"
                    @click="toggleExpandAll"
                    >展开/折叠
                </el-button>
            </el-col>
        </el-row>

        <el-table
            v-if="refreshTable"
            v-loading="loading"
            :data="areaList"
            row-key="id"
            :default-expand-all="isExpandAll"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        >
            <el-table-column prop="areaName" label="区域名称" min-width="260"></el-table-column>
            <el-table-column prop="areaCode" label="区域代码" min-width="200"></el-table-column>
            <el-table-column label="修改时间" align="center" prop="updateTime" min-width="200">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-plus"
                        @click="handleAddChildren(scope.row)"
                        v-show="scope.row.areaLevel < 4"
                        >新增下级
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-edit"
                        @click="handleUpdate(scope.row)"
                        >修改
                    </el-button>
                    <el-button
                        size="mini"
                        type="text"
                        icon="el-icon-delete"
                        @click="handleDelete(scope.row)"
                        >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
  
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
  
        <!-- 添加或修改区域配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <!-- <el-form-item label="上级区域" prop="areaParent" v-show="title==='添加下级区域'">
                    <el-input v-model="form.areaParent" placeholder="请输入上级区域" :disabled="true"/>
                </el-form-item> -->
                <div v-show="title==='添加下级区域'" style="padding-left: 30px;padding-bottom: 20px;">上级区域：{{ form.areaParentName }}</div>
                <el-form-item label="区域名称" prop="areaName">
                    <el-input v-model="form.areaName" placeholder="请输入区域名称"/>
                </el-form-item>
                <el-form-item label="区域代码" prop="areaCode">
                    <el-input v-model="form.areaCode" placeholder="请输入区域代码"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
</template>
  
<script>
import { listArea, addArea, updateArea, delArea } from '@/api/systemConfig/area'
  
export default {
    name: 'region',
    dicts: [],
    data() {
        return {
            // 遮罩层
            // loading: true,
            loading: false,
            // 总条数
            total: 0,
            // 区域表格数据
            areaList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                // areaParent: [
                //     { required: true, message: '上级区域不能为空', trigger: 'blur' }
                // ],
                areaName: [
                    { required: true, message: '区域名称不能为空', trigger: 'blur' }
                ],
                areaCode: [
                    { required: true, message: '区域代码不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询区域列表 */
        getList() {
            this.loading = true
            listArea(this.queryParams).then(response => {
                this.areaList = response.rows
                this.total = response.total
                this.loading = false
            }
            )
        },

        /** 展开/折叠操作 */
        toggleExpandAll() {
            this.refreshTable = false
            this.isExpandAll = !this.isExpandAll
            this.$nextTick(() => {
                this.refreshTable = true
            })
        },

        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },

        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                areaParent: undefined,
                areaName: undefined,
                areaCode: undefined
            }
            this.resetForm('form')
        },

        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加区域'
        },

        /** 新增下级按钮操作 */
        handleAddChildren(row) {
            this.reset()            
            this.form.areaParentName = row.areaName
            this.form.areaParent = row.areaCode
            this.open = true
            this.title = '添加下级区域'
        },

        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            this.form.id = row.id
            this.form.areaLevel = row.areaLevel
            this.form.areaParent = row.areaParent
            this.form.areaName = row.areaName
            this.form.areaCode = row.areaCode
            this.open = true
            this.title = '修改区域'
        },

        /** 提交按钮 */
        submitForm: function() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateArea(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addArea(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        /** 删除按钮操作 */
        handleDelete(row) {
            // this.$modal.confirm('是否确认删除区域代码为"' + row.areaCode + '"的数据项？').then(function() {
            this.$modal.confirm('是否确认删除该数据项？').then(function() {
                return delArea(row.areaCode)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        }

    }
}
</script>